import React from 'react';
import { inject, observer } from 'mobx-react';
import {WeaTableNew} from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import allStores from '../stores/index'

import { WeaErrorPage, WeaTools } from 'ecCom';
import {WeaTop,WeaTab,WeaRightMenu} from 'ecCom';
import {Input,Button,Alert,Spin,Icon,Modal,message} from 'antd';
import {Condition} from './ListCondition';

import isEmpty from 'lodash/isEmpty'
import forEach from 'lodash/forEach'
import {routerShape} from 'react-router';
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import FnaPermission from '../public/FnaPermission';
import  * as openUrl from '../util/pulic-func';

/**
 * 部门收支
 */
@inject('fnaExpenseDeptStore')
@observer
class FnaExpenseDept extends React.Component {
	static contextTypes = {
	    router: routerShape
	};

	constructor(props) {
		super(props);
	}

	componentDidMount() {
		//一些初始化请求
		const { fnaExpenseDeptStore } = this.props;	
        const {appendBaseParams,initDatas,doSearch,setRouter} = fnaExpenseDeptStore;		
		
		let {router} = this.context;
        setRouter(router);	
		initDatas();
		
	}
	
	componentWillReceiveProps(nextProps) {
		
	}
	shouldComponentUpdate(nextProps, nextState) {
		
		return true;
	}
	componentWillUnmount() {
		//组件卸载时一般清理一些状态

	}

	render() {
		
		const { fnaExpenseDeptStore } = this.props;
		const {title,showSearchAd,topTab,topTabCount,searchParams,form,dataKey,tableStore,loading,hasRight} = fnaExpenseDeptStore;
		const formParams = form.getFormParams() || {};
		return (
			<div style={{height:'100%'}}>
				{hasRight == 1 && <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@870tg5`} datas={this.getRightMenu()} collectParams={{favname:getLabel(28111,'收藏'), favouritetype:5}} >
				<WeaTop ecId={`${this && this.props && this.props.ecId || ''}_WeaTop@4if4s8`}
					title={getLabel(15403,'部门收支')}
					loading={loading}
					icon={<i className='icon-coms-Finance'/>}
					iconBgcolor='#f14a2d'
					buttons={this.getTopButton()}
					buttonSpace={10}
					showDropIcon={true}
					dropMenuDatas={this.getRightMenu()}
				>
		
				<WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@zgchej`}
					buttonsAd={this.getAdButtons()}
					searchType={['base','advanced']}
					searchsBaseValue=""//默认值
					setShowSearchAd={this.setShowSearchAd}
                    hideSearchAd={this.doHideSearchAd}
                    searchsAd={
                            <Condition ecId={`${this && this.props && this.props.ecId || ''}_Condition@eh7wl0`} listStore={fnaExpenseDeptStore} onEnterSearch={this.enterSearch}/>
                    }
                    showSearchAd={showSearchAd}
                    datas={topTab}
                    keyParam="index"  //主键
                    selectedKey={searchParams.index}
                    counts={topTabCount}
                    onChange={this.switchTab}
                    onSearch={this.baseSearch}
                    leftStyle={{width:'900px'}}
                    advanceHeight={150}
				/>
		
				<WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@wf1xm8`} 
                    comsWeaTableStore={tableStore}
                	hasOrder={true}
                	needScroll={true} 
                	onOperatesClick={this.operator}
                	getColumns={this.getColumnsInfo}
                />
				</WeaTop>
				</WeaRightMenu>}
				{hasRight == 4 && <FnaPermission ecId={`${this && this.props && this.props.ecId || ''}_FnaPermission@p3vjzf`}/>}
			</div>
		)
	}

	doHideSearchAd = () => {
		this.props.fnaExpenseDeptStore.setShowSearchAd(false);
    }
	
	setShowSearchAd = (bool) => {
		this.props.fnaExpenseDeptStore.setShowSearchAd(bool);
	}

	getColumnsInfo = (data) => {
		const that = this;
		/*
		if(!isEmpty(data)){
			for(let index=0;index<data.length;index++){
	      	}
		}
		*/
	}

	getAdButtons = () => {
		const { fnaExpenseDeptStore } = this.props;
		const { doSearch,setShowSearchAd,clearFormFields,tabKey,searchParams,topTab } = fnaExpenseDeptStore;

		let yearObj = topTab.find((item)=>{
			return searchParams.index === item.index;
		});

		if(!yearObj){
			yearObj = {
				title : ''
			};
		}

		return [
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@6kdgw1@Search`} type="primary" onClick={()=>{doSearch({fnayear:yearObj.title});setShowSearchAd(false);}}>{getLabel(197,'搜索')}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@91h4dt@Reset`} type="ghost" onClick={()=>{clearFormFields(tabKey);}}>{getLabel(2022,'重置')}</Button>),
			(<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3sbnv2@Cancel`} type="ghost" onClick={()=>{setShowSearchAd(false)}}>{getLabel(31129,'取消')}</Button>)
		];
	}

	getRightMenu(){
		const { fnaExpenseDeptStore } = this.props;
		const { dataKey,tableStore,setLoading } = fnaExpenseDeptStore;
		let btns = [];
		btns.push({
            key:"colSet",
    		icon: <i className='icon-coms-Custom'/>,
            content:getLabel(32535,'显示列定制'),
            onClick: key =>{
                tableStore.setColSetVisible(dataKey,true);
    		    tableStore.tableColSet(dataKey,true);
    		    setLoading(false);
            }
    	});
		return btns;
	}

	getTopButton(){
		let btns = [];
        return btns;
	}

	switchTab = (tabKey) => {
        const {fnaExpenseDeptStore} = this.props;
        const {topTab,searchParams} = fnaExpenseDeptStore;
        searchParams.index = topTab[tabKey].index;
        fnaExpenseDeptStore.doSearch({
           fnayear : topTab[tabKey].title
        });
    }

    //基础搜索
    baseSearch = (value) => {
    	const {fnaExpenseDeptStore} = this.props;
        const {topTab,searchParams} = fnaExpenseDeptStore;
        const fnayear = topTab[searchParams.index].title;
    	let data = Object.assign({
    		nameQuery:value,
    		fnayear:fnayear
    	});
    	fnaExpenseDeptStore.doSearch(data);
    }

    //列表操作
	operator = (record,index,operate,flag) => {
		const {fnaExpenseDeptStore} = this.props;
		const {topTab,searchParams} = fnaExpenseDeptStore;
		//详情
		if(flag=='0'){
			let organizationid = "organizationid="+record.id;
			let organizationtype = "organizationtype=2";
			let budgetperiods = "budgetperiods="+topTab[searchParams.index].key

			let url = "/spa/fna/static4engine/engine.html#/main/budget/budgetingPaymentView?"+
			organizationtype+"&"+organizationid+"&"+budgetperiods+"&_key=1";
			window.open(url,"_blank");
		}
	}
	
	/**
	* 高级搜索回车搜索
	*/
	enterSearch = () => {
		const {fnaExpenseDeptStore} = this.props;
		const {doSearch,setShowSearchAd} = fnaExpenseDeptStore;

		doSearch();
		setShowSearchAd(false);
	}
}


export default WeaTools.tryCatch(React,
	props => <WeaErrorPage ecId={`${this && this.props && this.props.ecId || ''}_WeaErrorPage@8j7hxi`} msg={ props.error ? props.error : getLabel(385218,'对不起,该页面异常,请联系管理员!')} />,
	{error: ""}
)(FnaExpenseDept);




